package com.android.bookgarden.tabfragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.bookgarden.MainActivity;
import com.android.bookgarden.MyApplication;
import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseFragment;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.MsgNotifyBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.ui.AboutActivity;
import com.android.bookgarden.ui.CollectionActivity;
import com.android.bookgarden.ui.FamilyEducationFundUI;
import com.android.bookgarden.ui.LeanBookAcitity;
import com.android.bookgarden.ui.LoginActivity;
import com.android.bookgarden.ui.MsgNotifyListActivity;
import com.android.bookgarden.ui.MyCodeActivity;
import com.android.bookgarden.ui.MyInterestActivity;
import com.android.bookgarden.ui.PersonInfoActivity;
import com.android.bookgarden.ui.SettingActivity;
import com.android.bookgarden.ui.ShareActivity;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.CircleImageView;
import com.android.bookgarden.views.LoadingDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.lzy.okgo.model.HttpHeaders;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by zouqh on 2018/3/19.
 * 我的
 */

public class Tab4Fragment extends BaseFragment implements View.OnClickListener,RealtConstract.View {


    @BindView(R.id.setting)
    ImageView setting;
    @BindView(R.id.message)
    ImageView message;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.ediutInfo)
    TextView ediutInfo;
    @BindView(R.id.mssageNum)
    TextView mssageNum;

    @BindView(R.id.relativelayou1)
    RelativeLayout relativelayou1;
    @BindView(R.id.jjMoney)
    TextView jjMoney;
    @BindView(R.id.relativelayou2)
    RelativeLayout relativelayou2;
    @BindView(R.id.relativelayou3)
    RelativeLayout relativelayou3;
    @BindView(R.id.relativelayou7)
    RelativeLayout relativelayou7;
    @BindView(R.id.relativelayou4)
    RelativeLayout relativelayou4;
    @BindView(R.id.relativelayou5)
    RelativeLayout relativelayou5;
    @BindView(R.id.relativelayou6)
    RelativeLayout relativelayou6;
    @BindView(R.id.headImg)
    CircleImageView headImg;
    Unbinder unbinder;
    private RealtConstract.Presenter presenter;
    private int pageIndex = 1;
    private int pageSize = 80;
    private List<MsgNotifyBean> list;
    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment;
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        presenter = new BasePersenter(this,getActivity());
        relativelayou1.setOnClickListener(this);
        relativelayou2.setOnClickListener(this);
        relativelayou3.setOnClickListener(this);
        relativelayou4.setOnClickListener(this);
        relativelayou5.setOnClickListener(this);
        relativelayou6.setOnClickListener(this);
        relativelayou7.setOnClickListener(this);
        setting.setOnClickListener(this);
        message.setOnClickListener(this);
        headImg.setOnClickListener(this);
        name.setOnClickListener(this);
        ediutInfo.setOnClickListener(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        if (null!=MyApplication.getInstance().userInfo){
            name.setText(""+MyApplication.getInstance().userInfo.getNickname());
            Glide.with(getActivity())
                    .load(""+MyApplication.getInstance().userInfo.getHeader())
                    .placeholder(R.mipmap.user_pic)
                    .error(R.mipmap.user_pic)
                    .dontAnimate()
                    .centerCrop()
                    .into(headImg);
            if (list.size()<=0){
                getDataList();

            }
        }else{
            list.clear();
            mssageNum.setVisibility(View.INVISIBLE);
            name.setText("请先登录");
            headImg.setImageResource(R.mipmap.user_pic);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.relativelayou1:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
            startActivity(MyCodeActivity.class);
                break;
            case R.id.relativelayou2:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
            startActivity(FamilyEducationFundUI.class);
                break;
            case R.id.relativelayou3:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
                startActivity(MyInterestActivity.class);
                break;
            case R.id.relativelayou4:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
            startActivity(LeanBookAcitity.class);
                break;
            case R.id.relativelayou5:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
                startActivity(AboutActivity.class);
                break;
            case R.id.relativelayou6:
            startActivity(ShareActivity.class);
                break;
            case R.id.relativelayou7:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
                startActivity(CollectionActivity.class);
                break;
            case R.id.setting:

                startActivity(SettingActivity.class);

                break;
            case R.id.message:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
                startActivity(MsgNotifyListActivity.class);
                break;
            case R.id.headImg:
                if (Utlis.isLogin(getActivity())){
                    return;
                }

                break;
            case R.id.ediutInfo:
                if (Utlis.isLogin(getActivity())){
                    return;
                }
                startActivity(PersonInfoActivity.class);
                break;
        }
    }


    private void getDataList() {

        presenter.loadDataHeader(null, "", Apis.MESSAGE_LIST + pageIndex + "/" + pageSize , 1002, Utlis.getHeadler(getActivity()), HttpType.GET);
    }

    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }
        LoadingDialog.hideProgress();
        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("请求失败");
        LoadingDialog.hideProgress();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {


                case 1002:
//                    refreshLayout.setRefreshing(false);
                    DataBean bean = Utlis.getDataBean(data);
                    if (null == bean) {
                        showToast("获取列表失败");
                        return;
                    }
                    if (bean.getCode() == 200) {
                        List<MsgNotifyBean> listD = JsonUitl.stringToList(bean.getData(), MsgNotifyBean.class);
                        if (null == listD || listD.size() <= 0) {
                            return;
                        }
                        list.clear();
                        list.addAll(listD);
                        int a=0;
                        for (int i = 0; i <list.size() ; i++) {
                            if (!list.get(i).isIsread()){
                                a++;
                            }
                        }
                        if (a>0){
                            mssageNum.setText(""+a);
                            mssageNum.setVisibility(View.VISIBLE);
                        }


                    } else {
//                        showToast(bean.getMessage() + "");
                    }

                    break;

            }
        }
    };

}
